package me.macd.mindoc.controller;

import me.macd.mindoc.dto.Result;
import me.macd.mindoc.model.Members;
import me.macd.mindoc.service.MemberService;
import me.macd.mindoc.util.constants.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @program: mindoc
 * @description: ${description}
 * @author: 2018-09-23
 * @create: 2018-09-23 10:49
 */
@Controller
@RequestMapping("/setting")
public class SettingController {

    @Autowired
    MemberService memberService;

    @RequestMapping(value = {"", "/"}, method = RequestMethod.GET)
    public String index(Model model) {
        model.addAllAttributes(Common.map("SettingController", "Index"));
        return "setting/index";
    }

    @RequestMapping(value = {"", "/"}, method = RequestMethod.POST)
    @ResponseBody
    public Result index(@RequestParam(value = "real_name", required = false) String realName,
                        @RequestParam("email") String email,
                        @RequestParam(value = "phone", required = false) String phone,
                        @RequestParam(value = "description", required = false) String description) {
        Members currentMember = Common.currentMember();
        Members members = new Members();
        members.setMemberId(currentMember.getMemberId());
        members.setRealName(realName);
        members.setEmail(email);
        members.setPhone(phone);
        members.setDescription(description);
        memberService.update(members);

        // 同时需要更新session
        currentMember.setRealName(realName);
        currentMember.setEmail(email);
        currentMember.setPhone(phone);
        currentMember.setDescription(description);

        return new Result(0, "ok");
    }

    @RequestMapping(value = "/password", method = RequestMethod.GET)
    public String password(Model model) {
        model.addAllAttributes(Common.map("SettingController", "Password"));
        return "setting/password";
    }

    @RequestMapping(value = "/password", method = RequestMethod.POST)
    @ResponseBody
    public Result password(@RequestParam("password1") String orgPassword,
                           @RequestParam("password2")String newPassword,
                           @RequestParam("password3")String rePassword) {
        Members currentMember = Common.currentMember();
        if (Constants.AUTH_METHOD_LDAP.equals(currentMember.getAuthMethod())) {
            return new Result(6009, "当前用户不支持修改密码");
        }

        if (StringUtils.isEmpty(orgPassword)) {
            return new Result(6003, "原密码不能为空");
        }

        if (StringUtils.isEmpty(newPassword)) {
            return new Result(6004, "新密码不能为空");
        }

        if (newPassword.length()<6 || newPassword.length()>18) {
            return new Result(6009, "密码必须在6-18字之间");
        }

        if (!newPassword.equals(rePassword)) {
            return new Result(6003, "确认密码不正确");
        }
        return new Result(0, "ok");
    }
}
